<template>
	<view>
		<!-- <view class="imgBox" v-if='img'>
			<image :src="img" class="img" mode="widthFix"></image>
		</view> -->
		<!-- #ifdef H5 || MP-WEIXIN -->
		<mumu-camera @success='handlerSuccess' :watermark='watermark'></mumu-camera>
		<!-- #endif -->

		<!-- #ifdef APP -->
		<appCamera @videoSuccessInfo='handlerSuccessVideo'></appCamera>

		<!-- #endif -->
	</view>
</template>

<script>
	import MumuCamera from '@/uni_modules/mumu-camera/components/mumu-camera/mumu-camera.vue'
	import appCamera from '@/components/appCamera/appCamera.vue'
	import FaceAPI from '@/utils/sdk/script.js';
	import {
		uploadBase64Image,
		uploadFilePromise
	} from '@/utils/utils.js';
	async function loadFile() {
		// 加载本地文件或网络文件
		await ffmpeg.load();
	}
	export default {
		components: {
			MumuCamera,
			appCamera
		},
		data() {
			return {
				real_avater: '',
				auth_avatar: '',
				watermark: [{
						type: 'text',
						x: 20,
						y: 40,
						font_color: "#fff",
						font_size: 14,
						text: '请正对屏幕微笑保持，保持面部在屏幕内'
					},

				],
				videoSrc: '',
				videoContext: ''
			}
		},
		onLoad(options) {
			this.auth_avatar = options.url
		},
		mounted() {
			this.videoContext = uni.createVideoContext('myVideo', this);
		},
		methods: {
			async handlerSuccessVideo(videoInfo) {
				console.log(videoInfo, 'videoInfo---')
				// this.handlerSuccess(this.videoSrc)

			},
			async handlerSuccess(img) {
				console.log("handlerSuccess", img)
				uni.showLoading({
					title: '正在上传'
				});

				try {
					// 上传base64图片
					const uploadResult = await uploadFilePromise(img);
					// 直接从返回结果中获取URL
					if (uploadResult.fullurl) {
						var img2 = uploadResult.fullurl;
					}
					if (uploadResult.url) {
						this.real_avater = uploadResult.url;
					}

					uni.showLoading({
						title: '正在识别'
					});
					let that = this
					// 调用人脸属性识别API，使用上传后的图片URL
					FaceAPI.compareFace(img2, that.$OssUrl(that.auth_avatar), (result) => {
						if (result.data.Code == "InvalidImage.NotFoundFace") {
							that.$Toast(result.data.Message);
						} else {
							uni.redirectTo({
								url: '/pages/authentication/avatar-similarity?Confidence=' + result
									.data.Data.Confidence.toFixed(2) + "&real_avater=" + that
									.real_avater + "&auth_avatar=" + that.auth_avatar,
								success: () => {
									console.log('跳转成功');
								},
								fail: (err) => {
									console.error('跳转失败:', err);
									that.$Toast('跳转失败，请重试');
								}
							});
						}
						uni.hideLoading();
					});
				} catch (error) {
					uni.hideLoading();
					console.error('上传失败：', error);
					this.$Toast('上传失败，请重试');
				}
			}
		}
	}
</script>
<style scoped>
	.video-preview {
		width: 100%;
		margin-bottom: 30rpx;
		border: 1rpx solid #eee;
	}
</style>